﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ycdlib
{
	internal class YCNodeAnother : YCNodeBase
	{
		string type;
		List<YCNode> children;

		static byte[] _eq = { 0x28, 0x0a };
		static byte[] _quot = { 0x29, 0x0a };

		#region YCNode メンバ

		/// <summary>
		/// 型の取得ができる
		/// </summary>
		public override string YCType
		{
			get { return type; }
			internal set { type = value; }
		}



		/// <summary>
		/// 値の取得、または設定ができる
		/// </summary>
		public override object Value
		{
			get { throw new NotSupportedException(); }
			set { throw new NotSupportedException(); }
		}



		/// <summary>
		/// 子ノードの数を取得できる
		/// </summary>
		public override int ChildCount
		{
			get { return children.Count; }
		}


		public YCNode this[ int index ]
		{
			get { return children[index]; }
			set { children[index] = value; }
		}
		#endregion



		/// <summary>
		/// コンストラクタ
		/// </summary>
		/// <param name="t"></param>
		/// <param name="p"></param>
		public YCNodeAnother ( string t, YCNode p ) : base(p)
		{
			type = t;
			children = new List<YCNode>();
		}


		/// <summary>
		/// 子要素の追加
		/// </summary>
		/// <param name="newNode"></param>
		public override void AddChild ( YCNode newNode )
		{
			children.Add( newNode );
		}


		/// <summary>
		/// 出力する
		/// </summary>
		/// <param name="s"></param>
		/// <param name="p"></param>
		public override void Write ( System.IO.Stream s, int p )
		{
			Encoding __e = Encoding.ASCII;
			Encoding __u = Encoding.UTF8;

			for( int c=0 ; c<p ; ++c )
			{
				s.WriteByte( 9 );
			}

			//	型名を出力
			byte[] __buff = __e.GetBytes( type );
			s.Write( __buff, 0, __buff.Length );

			if( !string.IsNullOrEmpty(Name) )
			{
				//	' '
				s.WriteByte( 0x20 );

				//	値の名称を出力
				__buff = __u.GetBytes( Name.ToString() );
				s.Write( __buff, 0, __buff.Length );
			}

			if( !string.IsNullOrEmpty(ID) )
			{
				//	':'
				s.WriteByte( 0x3a );

				//	IDを出力
				__buff = __u.GetBytes( ID.ToString() );
				s.Write( __buff, 0, __buff.Length );
			}

			s.WriteByte( 10 );
			for( int c=0 ; c<p ; ++c )
			{
				s.WriteByte( 9 );
			}
			//	'('
			s.Write( _eq, 0, 2 );

			if( children.Count > 0 )
			{
				int I = children.Count;
				for( int i=0 ; i<I ; ++i )
				{
					children[i].Write( s, p+1 );
				}
			}
			//__buff = __e.GetBytes( val.ToString() );
			//s.Write( __buff, 0, __buff.Length );

			for( int c=0 ; c<p ; ++c )
			{
				s.WriteByte( 9 );
			}
			s.Write( _quot, 0, 2 );
		}

/*
		static public YCNodeAnother Parse ( YCNode parentNode, string type, string name, string id, Parser p )
		{
			YCNodeAnother __res = new YCNodeAnother( type, parentNode );
			__res.Name = name;
			__res.ID = id;


			//	値を読み込む
			object o = p.readValue();
//System.Diagnostics.Debug.WriteLine("value = " + o.ToString() );

			__res.Value = o;

			return __res;
		}
*/
	}
}
